﻿@using System.Globalization
@model  Model.ModelViewConfig
@{
    ViewBag.Title = "脚本配置";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{
    <style>
        body
        {
            font-size: 12px;
        }
        #copy
        {
            display: none;
            position: fixed;
        }
        .col
        {
            width: 300px;
            height: 310px;
            padding: 2px;
            line-height: 18px;
            border: 1px solid #cccccc;
            overflow: scroll;
        }
        .fx
        {
            width: 400px;
            height: 230px;
            padding: 2px;
            line-height: 18px;
            white-space: nowrap;
            overflow: scroll;
            border: 1px solid #cccccc;
        }
        .fx div
        {
            height: 18px;
        }
        .fxbj
        {
            background: #3399FF;
            color: #ffffff;
        }
        .tb
        {
            margin-left: 5px;
            border-top: 1px solid #8DB2E3;
            border-right: 1px solid #8DB2E3;
            background: #ffffff;
        }
        .tb td
        {
            padding-left: 3px;
            border-left: 1px solid #8DB2E3;
            border-bottom: 1px solid #8DB2E3;
            height: 25px;
        }
        .dqEdit
        {
            display: none;
        }
    </style>
    <script src="/Scripts/dragDrop.js" type="text/javascript"></script>
}
<script type="text/javascript">
    var UrlArray = $.getUrlArray();
    var id = "";
    var actpTag = 1;
    var IsModel = 0;
    var ColumnTypeList = new Array();
    var dqColumnList = new Array();
    $(function () {
        var parent = window.dialogArguments; //获取父页面 
        id = UrlArray["id"];
        if (id != "" && id) {
            $("#edit").val(parent.retData[id].ColumnScriptExtend);
            $(".columnName").html("字段：" + parent.retData[id].ColumnName);
            IsModel = parent.retData[id].ColumnIsModel;
        }
        if (UrlArray["tarColName"] != "" && UrlArray["tarColName"]) {
            $("#edit").val(UrlArray["tarColName"]);
            $(".columnName").html("字段：" + UrlArray["tarColName"]);
            IsModel = 0;
        }
        var dqOpt;
        if (UrlArray["dqid"] != "" && UrlArray["dqid"]) {
            id = UrlArray["dqid"];
            var model = parent.retData[id];
            $("#edit").val(model.DisplayName);
            $("#hid_GUID").val(model.GUID);
            $("#txt_ColumnDesc").val(model.ColumnDesc);
            $("#sel_LegalType").val(model.LegalType);
            $("#txt_Rules").val(model.Rules);
            dqOpt = model.Operator;
            if (model.UniqueConstraint == 1)
                $("#chk_UniqueConstraint").attr("checked", "checked");
        }
        var isDQ = 0;
        if (UrlArray.dqid || UrlArray.dqid == "") {
            isDQ = 1;
            $(".dqEdit").show();
            $(".columnName").html("数据质量：" + parent.txtDQ_Name.value);
            $.post("/DQConfig/GetOperatorList", function (result) {
                for (var i = 0; i < result.length; i++) {
                    var sed = "";
                    if (dqOpt == result[i].LegalOperator)
                        sed = " selected";
                    $("#sel_Operator").append("<option value='" + result[i].LegalOperator + "' " + sed + ">" + result[i].LegalOperator + "</option>");
                }
            });
        }
        $.post("/ModelConfig/GetViewColumn", { modelID: UrlArray["modelID"], isDQ: isDQ, isScript: 1, dbName: encodeURI(UrlArray["dbName"]) }, function (result) {
            var colHtml = new Array();
            for (var i = 0; i < result.rows.length; i++) {
                var modelTy = {};
                if (isDQ == 1) {
                    var col = "[" + result.rows[i].TableName + "].[" + result.rows[i].ColumnName + "]";
                    dqColumnList.push(col);
                    modelTy["ColumnName"] = col;
                    modelTy["ColumnType"] = result.rows[i].ColumnType;
                    colHtml.push("<div id='col" + i + "'>" + col + "</div>");
                } else {
                    colHtml.push("<div id='col" + i + "'>[" + result.rows[i].ColumnName + "]</div>");
                    modelTy["ColumnName"] = "[" + result.rows[i].ColumnName + "]";
                    modelTy["ColumnType"] = result.rows[i].ColumnType;
                }
                ColumnTypeList.push(modelTy);
            }
            $(".col").html(colHtml.join(""));
            colInit();
        });
        GetFun("", "");
    })
    function searchFun() {
        $(".fx").html("");
        GetFun($.trim($("#search").val()), $("#ActionType").val());
    }
    function GetFun(name, atp) {
        var acTP = new Array();
        $.post("/Function/GetList", { funName: name, actionType: atp, funType: 0, order: "", sort: "", rows: 0, page: 1 }, function (result) {
            var funHtml = new Array();
            for (var i = 0; i < result.rows.length; i++) {
                var fc = result.rows[i].ParamCount;
                var fpArr = new Array();
                for (var j = 0; j < fc; j++) {
                    fpArr.push(" <#expression> ");
                }
                if (acTP.indexOf(result.rows[i].ActionType) == -1) {
                    acTP.push(result.rows[i].ActionType);
                    funHtml.push("" + result.rows[i].ActionType + "");
                    if (actpTag == 1) {
                        $("#ActionType").append("<option value='" + result.rows[i].ActionValue + "'>" + result.rows[i].ActionType + "</option>");
                    }
                }
                funHtml.push("<div title='" + result.rows[i].Description + "'>" + result.rows[i].FunName + "(" + fpArr.join(",") + ")</div>");
            }
            actpTag = 0;
            $(".fx").html(funHtml.join(""));
            sel();
        });
    }
    function colInit() {
        $(".col div").mousedown(function () {
            $(".col div").removeClass("fxbj");
            $(this).addClass("fxbj");
        })
        $(".col div").each(function (i, d) {
            var t = i * 18; //拖动行高
            //$(this).css({ 'position': 'absolute', 'left': $(this).position().left, 'top': $(this).position().top + t });
            $(this).dragDrop();
        })
    }
    function sel() {
        $(".fx div").mousedown(function () {
            $(".fx div").removeClass("fxbj");
            $(this).addClass("fxbj");
            $("#Description").val($(this).attr("title"));
        })
        $(".fx div").each(function (i, d) {
            var t = i * 18; //拖动行高
            //$(this).css({ 'position': 'absolute', 'left': $(this).position().left, 'top': $(this).position().top + t });
            $(this).dragDrop();
        })
    }
    function add(v) {
        if (!isNaN(psn)) {
            var val = $("#edit").val();
            var s = val.substr(0, psn);
            var e = val.substr(psn, val.length)
            $("#edit").val(s + v + e);
            $("#edit").focus();
            psn = $("#edit").val().length;
        }
    }
    function save() {
        var fcValue = $.trim($("#edit").val());
        if (fcValue == "") {
            alert('脚本不能为空!');
            return false;
        }
        if (fcValue.indexOf("<#expression>") > 0) {
            alert('请录入表达式参数值!');
            return false;
        }
        if (UrlArray.tarColName || UrlArray.tarColName == "") {
            returnParent(fcValue);
            return false;
        }
        var rd = {};
        if (UrlArray.id || UrlArray.id == "") {
            rd["GUID"] = id;
            rd["ColumnScriptExtend"] = fcValue;
            rd["ColumnIsModel"] = 0;
        }
        if (UrlArray.dqid || UrlArray.dqid == "") {
            rd = jv.GetDivArray(".searchArea", 4);
            if (rd["UniqueConstraint"] == "1" && dqColumnList.indexOf(fcValue) == -1) {
                alert("含表达式的脚本不能设置唯一约束。");
                return false;
            }
            rd["ColumnName"] = fcValue;
            rd["DisplayName"] = fcValue;
            if (UrlArray["dqid"] != "") {
                rd["Edit"] = true;
            }
        }

        $.post("/ModelConfig/CheckModelScript", { scriptText: fcValue, columnTypeList: $.toJSON(ColumnTypeList) }, function (res) {
            if (res[0] == "1") {
                rd["ColumnType"] = res[1];
                returnParent(rd);
            }
            else
                alert("脚本编辑错误：" + res[0]);
        });

        return false;
    }
</script>
<div style="margin: 5px;">
    <table class="listTitle" id="link">
        <tr>
            <td width="15">
                <img src="/images/edit.png" />
            </td>
            <td width="300">
                插入脚本
            </td>
            <td align="right">
                <input type="button" class="button" value="完成" onclick="save();" />
            </td>
        </tr>
    </table>
    <div id="dragContainer" style="position: relative; border: solid efefef; height: 510px;
        overflow-y: scroll;">
        <table border="1" cellpadding="5" cellspacing="5" width="815" style="border-color: #efefef;">
            <tr>
                <td width="200" valign="top">
                    <div style="font-weight: bold; line-height: 25px;">
                        表字段</div>
                    <div class="col">
                    </div>
                </td>
                <td>
                    <table border="0" width="480">
                        <tr>
                            <td width="80" align="right">
                                查找函数：
                            </td>
                            <td>
                                <input type="text" id="search" onkeydown="searchFun()" style="width: 260px;" />
                                <select id="ActionType" onchange="GetFun($.trim($('#search').val()),this.value)" style="width: 120px;">
                                    <option value=''>-选择函数类型-</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                选择函数：
                            </td>
                            <td height="240">
                                <div class="fx">
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                函数说明：
                            </td>
                            <td>
                                <textarea id="Description" readonly="readonly" style="width: 400px;" rows="4"></textarea>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <table width="100%">
                        <tr>
                            <td>
                                编辑[ <span class="columnName"></span>] 脚本：
                            </td>
                            <td align="right">
                                <input type="button" class="button" onclick="$('#edit').val('')" value="清空脚本" />
                            </td>
                        </tr>
                    </table>
                    <textarea id="edit" style="width: 800px;" rows="4"></textarea>
                    <div id="editCopy" style="display: none">
                    </div>
                </td>
            </tr>
        </table>
        <div class="dqEdit">
            <table class="listTitle" id="link">
                <tr>
                    <td width="15">
                        <img src="/images/edit.png" />
                    </td>
                    <td width="300">
                        [ <span class="columnName"></span>] 其他配置
                    </td>
                    <td align="right">
                    </td>
                </tr>
            </table>
            <table class="searchArea" width="100%">
                <tr>
                    <td align="right">
                        条件描述：
                    </td>
                    <td>
                        <input type="hidden" id="hid_GUID" />
                        <input type="text" id="txt_ColumnDesc" style="width: 400px;" /><br />
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        操作符：
                    </td>
                    <td>
                        <input type="hidden" id="sel_LegalType" value="非法规则" />
                        <select id="sel_Operator" class="sel">
                            <option value="">-请选择-</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right" valign="top">
                        规则：
                    </td>
                    <td>
                        <input type="text" id="txt_Rules" style="width: 400px;" /><br />
                        （例：设置条件字段Name=abc，符合条件的数据将被过滤到数据质量数据集）
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        唯一约束：
                    </td>
                    <td>
                        <input type="checkbox" id="chk_UniqueConstraint" value="1" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div id="copy">
        &nbsp</div>
</div>
